interface HeroData<T> {
  code: number;
  data: T;
  msg: string;
}
interface heroDetail {
  hero: heroText;
  skins: heroImg[];
}
interface heroText {
  shortBio: string;
  name: string;
}
interface heroImg {
  mainImg: string;
}

// pages/heroDetails/heroDetails.ts
Page({
  /**
   * 页面的初始数据
   */
  data: {
    text: "",
    image: [] as heroImg[],
    //
    activeIndex: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    this.getHeroData(options.heroId!).then((res) => {
      // console.log(res.hero.shortBio);
      // console.log(res.skins);
      this.setData({
        text: res.hero.shortBio,
        image: res.skins,
      });
    });
  },

  // 获取英雄列表
  getHeroData(id: string) {
    return new Promise<heroDetail>((resolve, reject) => {
      wx.showLoading({ title: "拼命加载中..." });
      wx.request<HeroData<heroDetail>>({
        method: "GET",
        url: "https://api-vue-base.itheima.net/api/lol/info",
        data: { id },
        success: (res) => {
          resolve(res.data.data);
          // resolve(res.data.data.hero);
          // resolve(res.data.data.skins);
          wx.setNavigationBarTitle({
            title: res.data.data.hero.name
          })
        },
        fail: () => {
          reject();
        },
        complete: () => {
          wx.hideLoading();
        },
      });
    });
  },

  // 自定义轮播图，指示点
  onSwiperChange(event: WechatMiniprogram.SwiperChange) {
    // console.log(event.detail.current);
    this.setData({
      activeIndex: event.detail.current,
    });
  },
});
